/**
 * Created by Administrator on 2017/2/8.
 */
var goods_id = getQueryString('goods_id');
var num = getQueryString('buynum');
$(function () {

    var key = getCookie('key');

    function initPage() {
        $.ajax({
            type: 'get',
            url: ApiUrl + "/index.php?act=goods&op=pdinfo",
            data: {goods_id:goods_id,key: key},
            dataType: 'json',
            success: function (result) {
                console.log(result);
                if(result['code']==200){
                    //商品图片格式化数据
                    if(result.datas.prodinfo.pgoods_image){
                        var goods_image = result.datas.prodinfo.pgoods_image.split(",");
                        result.datas.prodinfo.pgoods_image = goods_image;
                    }else{
                        result.datas.prodinfo.goods_image = [];
                    }
                    result.datas.prodinfo.num = num;
                    var html_image = template.render('details_tpl',result.datas);
                    $("#buy").html(html_image);

                    //图片轮播
                    // var swiper = new Swiper('.swiper-container', {
                    //     pagination: '.swiper-pagination',
                    //     paginationClickable: true
                    // });
                    // var swiper = new Swiper('.swiper-container');
                }
            }
        });
    }

    //点击商品规格，获取新的商品
    function arrowClick(self,myData){
        //拼接属性
        var curEle = $(".ultwo").find("li.active");
        var curSpec = [];
        $.each(curEle,function (i,v){
            // convert to int type then sort
            curSpec.push(parseInt($(v).attr("specs_value_id")) || 0);
        });
        var spec_string = curSpec.sort(function(a, b) { return a - b; }).join("|");
        console.log(myData.spec_list);
        //获取商品ID
        goods_id = myData.spec_list[spec_string];
        initPage();
    }

//初始化页面
    initPage();
});

function buyNow(){
    param = '';
    if (goods_id){
        param += "goods_id="+goods_id;
    }
    if (num){
        param += "&buynum="+num;
    }
    var url = WapSiteUrl+"/tmpl/order/point_buy_setp.html?"+param;
    window.location.href = url;
}